articles

Home / DeveloperSection / Articles / Event Receiver in SharePoint 2010

Event Receiver in SharePoint 2010

Chris Anderson14301 12-Dec-2011

Microsoft Visual Studio 2010 provides a project type that enables you to build event receivers that perform actions before or after selected events on a Microsoft SharePoint 2010 site. We can create Event Receivers (Event Handlers) to take care of things for us when specific things happen. For example, we can subscribe to the "ItemAdded" event for a specific list, and have our custom code execute when the event fires.

In this article I am going to explain how to create a simple Item added event receiver for a custom list in SharePoint 2010. The event receiver will change the Title of the item to current year Time once it’s added to the list. We will create and deploy the solution as a farm and will run in debug mode.

·         Open Visual Studio 2010 and create a new Project.

·         In project types select Event Receiver and give it a name.

·         Select .net framework 3.5.

Event Receiver in SharePoint 2010

Next in the SharePoint Customization Wizard, choose deploy as a farm solution and specify the site URL where you want the event handler to be deployed as shown in the below figure:

Event Receiver in SharePoint 2010

Next, in Event Receiver Settings dialog select the “List Item Events” as a type of event receiver and “Custom List” in the Item source dropdown. Select “An Item was Added” from Handle the following events as shown below:

Event Receiver in SharePoint 2010

Next, we will write some code to change the Title of an item to the current year when it is added to a custom list.

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
 
namespace EventHandlinginSP.EventReceiver1
{
    public class EventReceiver1 : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPListItem _currentItem = properties.ListItem;
            _currentItem["Title"] = DateTime.Now.Year;
            _currentItem.Update();
        }
    }
}
 

To build and deploy an application press “F5″.

Once press F5 the solution gets deployed to your site and your Visual studio project goes into a debug mode. Now we will create a new custom List “My List” and add a new item to test our handler.

Create List:

Event Receiver in SharePoint 2010

Event Receiver in SharePoint 2010


Create a New Item -

Event Receiver in SharePoint 2010

Once you add the Item you will see the results.

Event Receiver in SharePoint 2010

Thanks for reading this article. I think after reading this article you can easily implement event handler for SharePoint sites.



Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By